package controle.Curso;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import modelagem.Curso;
import controle.conexao;

public class cursoDAO {
	public long inserir(Curso curso) {
		long id = -1;
		
				
		String sql = "insert into cont_academico."
				+ "curso(nome, codigo, cargahoraria, valor)" 
				+ "values(?,?,?,?)";
		
		Connection connection = conexao.getInstace().getConnection();
		PreparedStatement statement = null;
		try {
			statement = connection.prepareStatement(sql,
					PreparedStatement.RETURN_GENERATED_KEYS);
			
			statement.setString(1, curso.getNome());
			statement.setString(2, curso.getCodigo());
			statement.setInt(3, curso.getCargaHoraria());
			statement.setDouble(4, curso.getValor());
			
			statement.executeUpdate();
			ResultSet set = statement.getGeneratedKeys();
			
			while (set.next()) {
				id = set.getLong("id");
			}
			JOptionPane.showMessageDialog(null, "Salvo com sucesso!");
		} catch (SQLException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.getMessage());
		}
		return id;
	}
	
	
	
	public List<Curso> buscar(String nome){
		String sql;
		Curso curso;
		List<Curso> lista = new ArrayList<Curso>();
			sql = "select * from cont_academico."
				+ "curso where nome like ? order by nome;";
		
		Connection connection = conexao.getInstace().getConnection();
		PreparedStatement statement = null;
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, "%"+nome+"%");
			statement.execute();
			ResultSet set = statement.getResultSet();
			
			while (set.next()) {
				curso = new Curso();
				curso.setId(set.getInt("id"));
				curso.setNome(set.getString("nome"));
				curso.setCodigo(set.getString("codigo"));
				curso.setCargaHoraria(set.getInt("cargahoraria"));
				curso.setValor(set.getDouble("valor"));
				lista.add(curso);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.getMessage());
		}
		return lista;
	}
	
	
	
	public void atualizar(Curso curso){
		String sql = "update cont_academico."
				+ "curso set (nome, codigo, cargahoraria, valor)" 
				+ "= (?,?,?,?) where id = ?";
		
		Connection connection = conexao.getInstace().getConnection();
		PreparedStatement statement = null;
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, curso.getNome());
			statement.setString(2, curso.getCodigo());
			statement.setInt(3, curso.getCargaHoraria());
			statement.setDouble(4, curso.getValor());
			statement.setInt(5, curso.getId());
			statement.execute();
			
			JOptionPane.showMessageDialog(null, "Atualizado com sucesso!");
		} catch (SQLException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.getMessage());
		}
	}
	
	public void remover(Curso curso){
		String sql = "delete from cont_academico."
				+ "curso where id = ?;";
		
		Connection connection = conexao.getInstace().getConnection();
		PreparedStatement statement = null;
		try {
			statement = connection.prepareStatement(sql);
			statement.setInt(1, curso.getId());
			statement.execute();
			
			JOptionPane.showMessageDialog(null, "Atualizado com sucesso!");
		} catch (SQLException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.getMessage());
		}
	}



	public Curso buscarPorId(int id_curso) {
		String sql;
		Curso curso = null;
			sql = "select * from cont_academico."
				+ "curso where id = ? order by nome;";
		
		Connection connection = conexao.getInstace().getConnection();
		PreparedStatement statement = null;
		try {
			statement = connection.prepareStatement(sql);
			statement.setInt(1, id_curso);
			statement.execute();
			ResultSet set = statement.getResultSet();
			
			while (set.next()) {
				curso = new Curso();
				curso.setId(set.getInt("id"));
				curso.setNome(set.getString("nome"));
				curso.setCodigo(set.getString("codigo"));
				curso.setCargaHoraria(set.getInt("cargahoraria"));
				curso.setValor(set.getDouble("valor"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.getMessage());
		}
		return curso;
	}

}
